<?php
/**
/test-format?format=json → 应该返回JSON格式

/test-format?format=xml → 应该返回XML格式
 */

namespace app\controllers;


use yii\filters\ContentNegotiator;
use yii\web\Controller;
use yii\web\Response;

class Response2Controller extends Controller
{
    public function behaviors()
    {
        //formatParam 和  formats
        return [
            'contentNegotiator' => [
                'class' => ContentNegotiator::className(),
                'formats' => [
                 'application/json' => Response::FORMAT_JSON,
                 'application/xml' => Response::FORMAT_XML,
                 'text/html'        => Response::FORMAT_HTML,
                ],
                'formatParam' => 'format', // 自定义参数名
//        'languages' => [ // 支持语言协商
//            'zh-CN',
//            'en-US',
//        ],
//        'languageParam' => '_lang', // 自定义语言参数名
        ]] + parent::behaviors();
    }

    /**
     *
     */
    public function actionIndex()
    {
        $data = ['data' => ['id' =>1], 'msg' => 'work!'];
        return $data;
    }
}